package cn.nxcoder.jmw.controller.activemq;

import cn.nxcoder.jmw.core.JsonResult;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;

/**
 * @author xiaqing
 */
@RestController
@Api(tags = "ActiveMQ-Queue模式请求接口")
@RequestMapping("/activemq/producer")
public class Producer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    private static final Logger LOG = LoggerFactory.getLogger(Producer.class);

    /**
     * 发送消息
     *
     * @param msg 待发送的消息
     */
    @GetMapping("/sendMsg/{msg}")
    public JsonResult<Void> sendMessage(@PathVariable(value = "msg") String msg) {
        jmsMessagingTemplate.convertAndSend(queue, msg);
        LOG.info("Queue发送消息：{}", msg);
        return new JsonResult<>();
    }

//    @JmsListener(destination = "out.queue")
//    public void receiveQueue(String msg) {
//        LOG.info("从out.queue中获取的消息为： {}", msg);
//    }
}
